//package com.example.myapplicationktkt2.ui
//
//import android.os.Bundle
//import androidx.activity.enableEdgeToEdge
//import androidx.appcompat.app.AppCompatActivity
//import androidx.core.view.ViewCompat
//import androidx.core.view.WindowInsetsCompat
//import com.example.myapplicationktkt2.R
//
//class ComponentsDemoActivity : AppCompatActivity() {
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        enableEdgeToEdge()
//        setContentView(R.layout.activity_components_demo2)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
//    }
//}

package com.example.myapplicationktkt2.ui

import android.app.AlertDialog
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplicationktkt2.R
import com.example.myapplicationktkt2.adapter.SampleAdapter


/**
 * ComponentsDemoActivity演示了各种Android UI组件的使用
 * 包括RecyclerView、ListView（在对话框中）和AlertDialog。
 *
 * @author [作者]
 * @since [版本]
 */
class DialogRecyclerViewActivity : AppCompatActivity() {

    // UI组件声明
    private lateinit var recyclerView: RecyclerView
    private lateinit var listViewButton: Button
    private lateinit var recyclerViewButton: Button
    private lateinit var alertDialogButton: Button

    // 示例数据
    private val sampleData = listOf(
        "项目 1", "项目 2", "项目 3", "项目 4", "项目 5",
        "项目 6", "项目 7", "项目 8", "项目 9", "项目 10"
    )

    /**
     * Activity创建时调用的方法
     *
     * @param savedInstanceState 保存的实例状态
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dialog_recycler_view)

        initViews()
        setupListeners()
    }

    /**
     * 初始化视图组件
     */
    private fun initViews() {
        recyclerView = findViewById(R.id.recyclerView)
        listViewButton = findViewById(R.id.btnShowListView)
        recyclerViewButton = findViewById(R.id.btnShowRecyclerView)
        alertDialogButton = findViewById(R.id.btnShowAlertDialog)

        // 设置RecyclerView
        recyclerView.layoutManager = LinearLayoutManager(this)
        /**
         * Kotlin中的Lambda表达式语法
         * 在Kotlin中，当函数的最后一个参数是函数类型时，可以将Lambda表达式写在函数调用的外面，这就是所谓的尾随Lambda语法。
         * */
        recyclerView.adapter = SampleAdapter(sampleData) { item ->
            Toast.makeText(this, "点击了: $item", Toast.LENGTH_SHORT).show()
        }
    }

    /**
     * 设置监听器
     */
    private fun setupListeners() {
        listViewButton.setOnClickListener {
            showListViewDialog()
        }

        recyclerViewButton.setOnClickListener {
            showRecyclerViewDialog()
        }

        alertDialogButton.setOnClickListener {
            showAlertDialog()
        }
    }

    /**
     * 显示AlertDialog对话框
     */
    private fun showAlertDialog() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("提示")
        builder.setMessage("这是一个 AlertDialog 示例")

        // 添加确定按钮
        builder.setPositiveButton("确定") { dialog, _ ->
            Toast.makeText(this, "点击了确定按钮", Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }

        // 添加取消按钮
        builder.setNegativeButton("取消") { dialog, _ ->
            Toast.makeText(this, "点击了取消按钮", Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }

        // 添加中性按钮
        builder.setNeutralButton("稍后提醒我") { dialog, _ ->
            Toast.makeText(this, "点击了稍后提醒我按钮", Toast.LENGTH_SHORT).show()
            dialog.dismiss()
        }

        val dialog = builder.create()
        dialog.show()
    }

    /**
     * 显示包含ListView的对话框
     */
    private fun showListViewDialog() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("ListView 示例")

        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, sampleData)

        builder.setAdapter(adapter) { _, which ->
            Toast.makeText(this, "选择了: ${sampleData[which]}", Toast.LENGTH_SHORT).show()
        }

        builder.setNegativeButton("关闭") { dialog, _ ->
            dialog.dismiss()
        }

        val dialog = builder.create()
        dialog.show()
    }

    /**
     * 显示包含RecyclerView的对话框
     */
    private fun showRecyclerViewDialog() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("RecyclerView 示例")

        val recyclerView = RecyclerView(this)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = SampleAdapter(sampleData) { item ->
            Toast.makeText(this, "选择了: $item", Toast.LENGTH_SHORT).show()
        }

        builder.setView(recyclerView)

        builder.setNegativeButton("关闭") { dialog, _ ->
            dialog.dismiss()
        }

        val dialog = builder.create()
        dialog.show()
    }
}

